package ups.Wind;

import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.visitors.NodeVisitor;

import java.util.ArrayList;

public class WindVisitor extends NodeVisitor{
	
	private String date;
	private String temp;
	private String dir;
	private String speed;
	
	private int tblCount;
	private boolean inTR;
	private boolean on;
	
	private ArrayList<WindRecord> _records;
	public WindVisitor( ArrayList<WindRecord> list ) {
		_records = list;
		on = false;
	}
	
	public ArrayList<WindRecord> getRecords() {
		return _records;
	}
	
    public void visitTag (Tag tag)
    {
    	if( tag.getTagName().equals("TR") && on == true) {
    		tblCount = 0;
			inTR = true;
    	}
    //	if( Tag.getTagName().equals( "</TR"))
        System.out.println ("\n" + tag.getTagName () + " " + tag.getStartPosition ());
    }

    public void visitStringNode (Text string)
    {
		if( string.getText().equals( "Speed mph")) {
			on = true;
		}
    	if( inTR ) {
    		switch( tblCount ) {
    		case 0:
    			System.out.println("DATE: " + string.getText());
    			date = string.getText();
    			break;
    		case 1:
    			System.out.println("TEMP: " + string.getText());
    			temp = string.getText();
    			break;
    		case 2:
    			System.out.println("DIR: " + string.getText());
    			dir = string.getText();
    			break;
    		case 3:
    			System.out.println("SPD: " + string.getText());
    			speed = string.getText();
    			WindRecord rec = new WindRecord( date, temp, dir, speed );
    			_records.add( rec );

    			break;
    		default:
    			System.out.println( "Ran out of ideas *" + string.getText() + "*");
    		}

    		tblCount++;
    	}
    }
}
